package com.mcxx.modules.common.service.impl;

import com.mcxx.modules.common.service.StatisticService;
import com.mcxx.modules.statistics.dto.PovertyStatisticsDTO;
import com.mcxx.modules.statistics.dto.StatisticDTO;
import com.mcxx.modules.statistics.param.StatisticParam;
import com.mcxx.modules.statistics.service.read.StatisticReadService;
import com.mcxx.modules.system.entity.AreaSalvationEntity;
import com.mcxx.util.AreaUtil;
import com.mcxx.util.Constant;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
@TaTransactional
public class StatisticPovertyServiceImpl extends StatisticService {
    @Autowired
    private AreaUtil areaUtil;
    @Autowired
    private StatisticReadService statisticReadService;

    @Override
    public List<? extends StatisticDTO> getStatistic(StatisticParam param, UserAccountVo accountVo, String standType) {
        List<TaAreaPo> taAreaPos = new ArrayList<TaAreaPo>();
        List<Integer> levels = new ArrayList<Integer>();
        String name = super.commonStatisticStart(param, accountVo, taAreaPos, levels);

        List<PovertyStatisticsDTO> results = null;
        List<String> standards = null;
        if(standType.equals(Constant.BusinessStandType.CITY_TEKUN)) {
            name += "城市特困综合统计报表";
            standards = Arrays.asList("03", "16", "17", "18");
            results = statisticReadService.getCityPovertyComp(
                    areaUtil.getValidCode(taAreaPos.get(0).getAreaCode(), taAreaPos.get(0).getAreaLevel()),
                    levels, param.getMonth());
        }
        else{
            name += "农村特困综合统计报表";
            standards = Arrays.asList("04", "13", "14", "15");
            results = statisticReadService.getCountryPovertyComp(
                    areaUtil.getValidCode(taAreaPos.get(0).getAreaCode(), taAreaPos.get(0).getAreaLevel()),
                    levels, param.getMonth());
        }
        super.commonStatisticEnd(name, standards, taAreaPos, levels, results);

        List<PovertyStatisticsDTO> lastResults = results.stream().filter(e -> !e.getAreaCode().endsWith("99900")
                && !e.getAreaName().startsWith("测试") && !e.getAreaName().endsWith("省本级合计")).collect(Collectors.toList());
        return lastResults;
    }

    @Override
    public void fillSalvation(StatisticDTO dto, List<String> standards, List<AreaSalvationEntity> salivations) {
        for(AreaSalvationEntity salvation : salivations){
            if(salvation.getType().equals(standards.get(0))){
                ((PovertyStatisticsDTO)dto).setStandard(salvation.getStandardMoney());
            }
            else if(salvation.getType().equals(standards.get(1))){
                ((PovertyStatisticsDTO)dto).setAllSelStandard(salvation.getStandardMoney());
            }
            else if(salvation.getType().equals(standards.get(2))){
                ((PovertyStatisticsDTO)dto).setAllNurStandard(salvation.getStandardMoney());
            }
            else if(salvation.getType().equals(standards.get(3))){
                ((PovertyStatisticsDTO)dto).setHalfNurStandard(salvation.getStandardMoney());
            }
        }
    }
}
